package cn.fl.project.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjLnkCompRlPsnService;
import cn.fl.project.transform.PrjLnkCompRlPsnVOTransform;
import cn.fl.project.vo.PrjLnkCompRlPsnVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.riskctrl.facade.brjf.api.IBrjfTelCheckApiService;
import fl.riskctrl.facade.py.api.IPyExecutiveApiService;
import fl.riskctrl.facade.py.api.IPyTelCheckApiService;
import fl.riskctrl.facade.riskctrl.dto.DataResultDTO;
import fl.riskctrl.facade.td.api.ITdTelCheckApiService;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.lnkcomp.api.*;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompRlPsnQC;
import fl.spl.facade.lnkcomp.dto.LnkCompModDtlDTO;
import fl.spl.facade.lnkcomp.dto.PrjLcApplyRlPsnDTO;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompDTO;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompRlPsnDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 挂靠公司关联人员信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjLnkCompRlPsnServiceImpl implements IPrjLnkCompRlPsnService {

	@Autowired
	IPrjLnkCompRlPsnApiService prjLnkCompRlPsnApiService ;

	@Autowired
	IPrjLnkCompRlPsnQueryApiService prjLnkCompRlPsnQueryApiService ;
	
	/**
	 * 百融三要素核查
	 * */
	@Autowired
	private IBrjfTelCheckApiService brjfTelCheckApiService ;
	/**
	 * 鹏元三要素核查
	 * */
	@Autowired
	private IPyTelCheckApiService pyTelCheckApiService ;
	/**
	 * 同盾三要素核查
	 * */
	@Autowired
	private ITdTelCheckApiService tdTelCheckApiService ;
	/**
	 * 鹏元企业信息核查
	 * */
	@Autowired
	private IPyExecutiveApiService pyExecutiveApiService ;

	
	@Autowired
	private IPrjLcApplyRlPsnApiService prjLcApplyRlPsnApiService ;

	@Autowired
	private IPrjLnkCompQueryApiService prjLnkCompQueryApiService ;

	@Autowired
	private ILnkCompModDtlApiService nkCompModDtlApiService ;

	@Override
	public PrjLnkCompRlPsnVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("获取挂靠公司关联人员信息,id为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		PrjLnkCompRlPsnDTO dto =this.prjLnkCompRlPsnQueryApiService.selectById(id);
		return PrjLnkCompRlPsnVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjLnkCompRlPsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增挂靠公司关联人员信息不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		return this.prjLnkCompRlPsnApiService.addPrjLnkCompRlPsn(PrjLnkCompRlPsnVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("删除挂靠公司关联人员信息,id不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		}
		return this.prjLnkCompRlPsnApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newWebLogger("删除挂靠公司关联人员信息,id集合不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		};
		return prjLnkCompRlPsnApiService.removePrjLnkCompRlPsnByIds(ids);
    }

	@Override
	public boolean modify(PrjLnkCompRlPsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改挂靠公司关联人员信息,参数不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		}
		return this.prjLnkCompRlPsnApiService.modify(PrjLnkCompRlPsnVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.prjLnkCompRlPsnQueryApiService.searchListPage(page);
		List<PrjLnkCompRlPsnDTO> resultList = (List<PrjLnkCompRlPsnDTO>)pager.getResultList();
		List<PrjLnkCompRlPsnVO> voList = PrjLnkCompRlPsnVOTransform.toVOList(resultList);
		pager.setResultList(voList);
		return pager;
	}

	@Override
	public List<PrjLnkCompRlPsnVO> selectList(PrjLnkCompRlPsnQC qc) throws BaseException {
		List<PrjLnkCompRlPsnDTO> searchLists = this.prjLnkCompRlPsnQueryApiService.searchLists(qc);
		return PrjLnkCompRlPsnVOTransform.toVOList(searchLists);
	}
	@Override
	public DataResultDTO checkRlPsnVerify(Map<String, String> map) throws BaseException {
		// TODO Auto-generated method stub
			String modAttrCon = null ;
			String verifyCode = "2" ;
				DataResultDTO resultDTO = new DataResultDTO(BaseConstant.IS_YESNO_YES,"成功");
				if(CollectionUtils.isMapEmpty(map)){
					resultDTO.setInfo("数据为空");
					resultDTO.setSuccess(BaseConstant.IS_YESNO_NO);
					return resultDTO ;
				}
				Long dataId ;
				if(StringUtils.isNotEmpty(map.get("rlId"))){
					dataId = Long.valueOf(map.get("rlId"));
				}else if(StringUtils.isNotEmpty(map.get("applyPsnId"))){
					dataId = Long.valueOf(map.get("applyPsnId"));
				}else{
					resultDTO.setInfo("数据ID为空");
					resultDTO.setSuccess(BaseConstant.IS_YESNO_NO);
					return resultDTO ;
				}
				String code ;
				PrjLnkCompRlPsnDTO rlPsnDTO = new PrjLnkCompRlPsnDTO();
				PrjLcApplyRlPsnDTO prjLcApplyRlPsnDTO = new PrjLcApplyRlPsnDTO();
				if("verify".equals(map.get("type"))){
					PrjLnkCompDTO compDTO = this.prjLnkCompQueryApiService.selectPrjLnkCompByIdAndBuOrgCd(Long.valueOf(map.get("compId")), CurrentThreadContext.getCurrentOrgCd(), CurrentThreadContext.getCurrentSysCd());
					if("5".equals(map.get("rlTypCd"))){
						resultDTO = this.pyExecutiveApiService.getPersonShareholderInfoCheck(dataId, map.get("certNo"), map.get("rlNm"), compDTO.getLnkCompNm(), null);
					}else{
						resultDTO = this.pyExecutiveApiService.getPersonArtificialInfoCheck(dataId, map.get("certNo"), map.get("rlNm"), compDTO.getLnkCompNm(), null);
					}
					modAttrCon = "busInfoVerify";
					if(resultDTO != null && resultDTO.getSuccess() == 1){
						code = (String)resultDTO.getData();
						if("一致".equals(code)){
							rlPsnDTO.setBusInfoVerify("1");
							prjLcApplyRlPsnDTO.setBusInfoVerify("1");
							verifyCode = "1";
						}else{
							rlPsnDTO.setBusInfoVerify("-1");
							prjLcApplyRlPsnDTO.setBusInfoVerify("-1");
							verifyCode = "-1";
						}
					}else{
						rlPsnDTO.setBusInfoVerify("0");
						prjLcApplyRlPsnDTO.setBusInfoVerify("0");
						verifyCode = "0";
					}
				}
				if("TD".equals(map.get("type"))){
					resultDTO = this.tdTelCheckApiService.getTDTelCheck(dataId, map.get("certNo"), map.get("rlNm"), map.get("tel"));
					modAttrCon = "tdPerVerify";
					if(resultDTO != null && resultDTO.getSuccess() == 1){
						code = (String)resultDTO.getData();
						if("一致".equals(code)){
							rlPsnDTO.setTdPerVerify("1");
							prjLcApplyRlPsnDTO.setTdPerVerify("1");
							verifyCode = "1";
						}else{
							rlPsnDTO.setTdPerVerify("-1");
							prjLcApplyRlPsnDTO.setTdPerVerify("-1");
							verifyCode = "-1";
						}
					}else{
						rlPsnDTO.setTdPerVerify("0");
						prjLcApplyRlPsnDTO.setTdPerVerify("0");
						verifyCode = "0";
					}
				}
				if("PY".equals(map.get("type"))){
					resultDTO = this.pyTelCheckApiService.getPYTelCheck(dataId, map.get("certNo"), map.get("rlNm"), map.get("tel"));
					modAttrCon = "pyPerVerify";
					if(resultDTO != null && resultDTO.getSuccess() == 1){
						code = (String)resultDTO.getData();
						if("一致".equals(code)){
							rlPsnDTO.setPyPerVerify("1");
							prjLcApplyRlPsnDTO.setPyPerVerify("1");
							verifyCode = "1";
						}else{
							rlPsnDTO.setPyPerVerify("-1");
							prjLcApplyRlPsnDTO.setPyPerVerify("-1");
							verifyCode = "-1";
						}
					}else{
						rlPsnDTO.setPyPerVerify("0");
						prjLcApplyRlPsnDTO.setPyPerVerify("0");
						verifyCode = "0";
					}
				}
				if("BR".equals(map.get("type"))){
					resultDTO = this.brjfTelCheckApiService.getBRJFTelCheck(dataId, map.get("certNo"), map.get("rlNm"), map.get("tel"),null);
					modAttrCon = "brPerVerify";
					if(resultDTO != null && resultDTO.getSuccess() == 1){
						code = (String)resultDTO.getData();
						if("均一致".equals(code)){
							rlPsnDTO.setBrPerVerify("1");
							prjLcApplyRlPsnDTO.setBrPerVerify("1");
							verifyCode = "1";
						}else{
							rlPsnDTO.setBrPerVerify("-1");
							prjLcApplyRlPsnDTO.setBrPerVerify("-1");
							verifyCode = "-1";
						}
					}else{
						rlPsnDTO.setBrPerVerify("0");
						prjLcApplyRlPsnDTO.setBrPerVerify("0");
						verifyCode = "0";
					}
				}
				boolean b = false;
				if(StringUtils.isNotEmpty(map.get("applyId"))){
					LnkCompModDtlDTO modDtlDTO = new LnkCompModDtlDTO();
					modDtlDTO.setIsDel(BaseConstant.IS_YESNO_NO);
					modDtlDTO.setIsValid(BaseConstant.IS_YESNO_YES);
					modDtlDTO.setModAttrCon("三要素校验结果");
					modDtlDTO.setModAttrFiled(modAttrCon);
					modDtlDTO.setModConAft(verifyCode);
					modDtlDTO.setModBusId(dataId);
					modDtlDTO.setTabNm("PRJ_LNK_COMP_RL_PSN");
					modDtlDTO.setModTypCd("MDF");
					modDtlDTO.setModId(Long.valueOf(map.get("applyId")));
					nkCompModDtlApiService.add(modDtlDTO);
				}else if(StringUtils.isEmpty(map.get("applyPsnId"))){
					rlPsnDTO.setId(Long.valueOf(map.get("rlId")));
					rlPsnDTO.setMdfTm(new Date());
					rlPsnDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					b = this.prjLnkCompRlPsnApiService.modify(rlPsnDTO);
				}else{
					prjLcApplyRlPsnDTO.setId(Long.valueOf(map.get("applyPsnId")));
					prjLcApplyRlPsnDTO.setMdfTm(new Date());
					prjLcApplyRlPsnDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					b = this.prjLcApplyRlPsnApiService.modify(prjLcApplyRlPsnDTO);
				}
				return resultDTO;
	}

	@Override
	public Pager searchListPage(Pager page, int isMask) throws BaseException {
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.prjLnkCompRlPsnQueryApiService.searchListByMaskPage(page, isMask);
		List<PrjLnkCompRlPsnDTO> resultList = (List<PrjLnkCompRlPsnDTO>)pager.getResultList();
		List<PrjLnkCompRlPsnVO> voList = PrjLnkCompRlPsnVOTransform.toVOList(resultList);
		pager.setResultList(voList);
		return pager;
	
	}
}
